package com.peppa.petshop.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.peppa.petshop.entity.Category;
import com.peppa.petshop.mapper.CategoryMapper;
import com.peppa.petshop.service.CategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.peppa.petshop.vo.DataVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 张三
 * @since 2021-06-02
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired(required = true)
    private CategoryMapper categoryMapper;
    public DataVO<Category> categoryList(Integer page,Integer limit){
        if (page== null){
            page=1;
            limit = 10;
        }

        IPage<Category> categoryIPage = new Page<Category>(page,limit);
        IPage<Category> result = categoryMapper.selectPage(categoryIPage, null);
        List<Category> categories = result.getRecords();
        Integer count = (int) result.getTotal();
        DataVO<Category> categoryDataVO = new DataVO<Category>(0,"",count,categories);
        return categoryDataVO;
    }

    @Override
    public void deleteById(Integer id) {
        categoryMapper.deleteById(id);
    }

    @Override
    public void addCategory(String category) {
        Integer integer = null;
        categoryMapper.insert(new Category(integer,category,0, LocalDateTime.now()));
    }

    @Override
    public void editCategory(Integer id, String category) {
        categoryMapper.updateById(new Category(id,category,0,LocalDateTime.now()));
    }

}
